<template>
	<view class="content">
		<view class="search-box">
			<!-- mSearch组件 如果使用原样式，删除组件元素-->
			<mSearch class="mSearch-input-box" 
				:mode="2" 
				button="inside" 
				:placeholder="defaultKeyword"
				@search="doSearch(false)"
				@input="inputChange" 
				@confirm="doSearch(false)" 
				v-model="keyword">
			</mSearch>
			<!-- 原样式 如果使用原样式，恢复下方注销代码 -->

			<!-- <view class="input-box">
				<input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
				 placeholder-class="placeholder-class" confirm-type="search">
			</view>
			<view class="search-btn" @tap="doSearch(false)">搜索</view> -->

			<!-- 原样式 end -->
		</view>
		<view class="search-keyword" v-if="!showList">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<block v-for="(row,index) in keywordList" :key="index">
					<view class="keyword-entry" hover-class="keyword-entry-tap">
						<view class="keyword-text" @tap.stop="doSearch(keywordList[index])">
							<rich-text :nodes="row.htmlStr"></rich-text>
						</view>
						<view class="keyword-img" @tap.stop="setKeyword(index)">
							<image src="/static/search/back.png"></image>
						</view>
					</view>
				</block>

			</scroll-view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>历史搜索</view>
						<view>
							<image @tap="oldDelete" src="/static/search/delete.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword.title ? keyword.title : keyword.keyword}}</view>
					</view>
				</view>
				<view class="keyword-block">
					<view class="keyword-list-header">
						<view>热门搜索</view>
						<view>
							<image @tap="hotToggle" :src="'/static/search/attention'+forbid+'.png'"></image>
						</view>
					</view>
					<view class="keyword" v-if="forbid==''">
						<view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword.title}}</view>
					</view>
					<view class="hide-hot-tis" v-else>
						<view>当前搜热门搜索已隐藏</view>
					</view>
				</view>
			</scroll-view>
		</view>
		
		<view class="rank-shoplist" v-if="showList">
			<view class="van-list mt20">
				<!--v-for="(item, index) in itemList" :key="index"
					:data-id="index" -->
				<u-empty v-if="itemList==0" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" />
				<view class="g-shop-list g-df-vc" v-for="(item, index) in searchEngineList" :key="index" :data-id="index"
					@click="xdlActivity(item)">
					<view class="shop-img">
						<image class="sdl-image" :src="item.mainPic"> </image>
		
					</view>
		
					<view class="shop-box">
						<view class="shop-padding">
							<view
								style="box-sizing:border-box;display: flex;align-items: center;-webkit-box-align: center;">
		
								<text class="sdl-text">
									<text class="G-font g-gps"></text>{{item.city}}
								</text>
		
								<text
									style="font-size: 20rpx;display: flex;width: 50%;color: gray;justify-content: flex-end;">
									距你{{item.distance}}km
								</text>
		
							</view>
							<view class="act-title">{{item.title}}</view>
							<view class="act-content">
								{{item.actName}}
							</view>
						</view>
						<view class="shop-padding" style="margin-left: 10rpx;">
							<!--券专区-->
							<view>
								<view class="coupon-box g-df-vc">
									<view style="font-family: PingFang SC;color:#ac4040;">
										<text>¥</text>
										<text style="font-size: 32rpx;margin:0 8rpx;">{{item.price}}</text>
										<text style="color: #909399;font-weight: 400;">起</text>
									</view>
		
								</view>
								<view class="sell g-text-ellipsis">
									<view style="display: flex;align-items: center;width:50%;">
										<text class="G-font g-mall"></text>
										<text>{{item.shopName}}</text>
									</view>
									<view style="display: flex;justify-content:flex-end;width: 50%;">
										<text>{{item.cTime}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					<u-back-top right="20" bottom="100" icon="arrow-up" mode="circle" duration="300"
						:scrollTop="scrollTop"></u-back-top>
				</view>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	import utils from '@/common/utils.js'
	//引用mSearch组件，如不需要删除即可
	import mSearch from '@/components/mehaotian-search-revision/mehaotian-search-revision.vue';
	export default {
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				searchEngineList:[],
				showList:false,
				forbid: '',
				isShowKeywordList: false
			}
		},
		onLoad() {
			this.init();
		},
		components: {
			//引用mSearch组件，如不需要删除即可
			mSearch
		},
		methods: {
			init() {
				this.loadDefaultKeyword();
				this.loadOldKeyword();
				this.loadHotKeyword();

			},
			blur() {
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "默认关键字";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			loadHotKeyword() {
				//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
				//this.hotKeywordList = ['活动', '越野', '露营'];
				
				this.$Request.get(this.$api.activity.getHostList).then(res => {
					if (res.code === 20000 && res.data != null) {
						this.hotKeywordList = res.data;
					}
				})
				
				
			},
			//监听输入
			inputChange(event) {
				//兼容引入组件时传入参数情况
				this.showList=false;
				var keyword = event.detail ? event.detail.value : event;
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				console.log('keyword',keyword)
				const location = uni.getStorageSync("location");
				const params = {
					pageNo: 1,
					pageSize: 10,
					lat: location.lat,
					lng: location.lng,
					title: keyword.title
				}
				this.$Request.post(this.$api.search.searchEngine,params).then(res => {
					if (res.code == 20000) {
						this.keywordList = [];
						this.keywordList = this.drawCorrelativeKeyword(res.data, keyword);
						
					}
				})
			},
			//高亮关键字
			drawCorrelativeKeyword(keywords, keyword) {
				var len = keywords.length,
					keywordArr = [];
				for (var i = 0; i < len; i++) {
					var row = keywords[i];
					//定义高亮#9f9f9f
					var html = row.title.replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
					html = '<div>' + html + '</div>';
					var tmpObj = {
						keyword: row.title,
						htmlStr: html,
						id:row.id
					};
					keywordArr.push(tmpObj)
				}
				return keywordArr;
			},
			//顶置关键字
			setKeyword(index) {
				console.log('setKeyword',index)
				console.log(index)
				this.keyword = this.keywordList[index].keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			//执行搜索
			doSearch(keyword) {
				
				console.log('keyword',this.keyword)
				const location = uni.getStorageSync("location");
				let title="";
				if(keyword==false){
					title=this.keyword;
				}else{
					title=keyword.title ? keyword.title :keyword.keyword;
				}
				
				const params = {
					pageNo: 1,
					pageSize: 10,
					lat: location.lat,
					lng: location.lng,
					title: title
				}
				this.$Request.post(this.$api.search.searchEngine,params).then(res => {
					if (res.code == 20000) {
						this.searchEngineList=res.data;
						this.showList=true;
					}
				})
				//保存为历史 
				console.log('this.oldKeywordList',this.oldKeywordList)
				if(keyword!=false){
					const save= this.oldKeywordList.filter(item=>item.id==keyword.id);
					console.log(save)
					if(save==0){
						this.saveKeyword(keyword);
					}
				}
				
			},
			xdlActivity(v) {
				utils.removeCache("actInfo");
				utils.setCache('actInfo', v)
				uni.navigateTo({
					url: '/pages/activity/components/detail?id=' + v.id
				})
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				console.log('saveKeyword',keyword)
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>
<style lang="scss">
	view {
		display: block;
	}

	.search-box {
		width: 95%;
		background-color: rgb(242, 242, 242);
		padding: 15upx 2.5%;
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
	}

	.search-box .mSearch-input-box {
		width: 100%;
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 2%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: 28upx;
		color: #fff;
		background: linear-gradient(to right, #ff9801, #ff570a);
		border-radius: 60upx;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60upx;
		font-size: 32upx;
		border: 0;
		border-radius: 60upx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
	}

	.placeholder-class {
		color: #9e9e9e;
	}

	.search-keyword {
		width: 100%;
		background-color: rgb(242, 242, 242);
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 3%;
		font-size: 30upx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 110upx);
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 94%;
		padding: 10upx 3%;
		font-size: 27upx;
		color: #333;
		display: flex;
		justify-content: space-between;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: 28upx;
		color: #6b6b6b;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 60upx;
		font-size: 28upx;
		background-color: rgb(242, 242, 242);
		color: #6b6b6b;
	}
	
	
	/**活动列表 */
	.rank-shoplist {
		padding-top: 20upx;
	
		.van-list {
			display: flex;
			flex-wrap: wrap;
			-webkit-box-align: end;
			align-items: flex-end;
			-webkit-box-pack: center;
			justify-content: center;
			padding: 0 20rpx;
	
			.g-shop-list {
				width: 100%;
				margin: 0 auto;
				margin-top: 10rpx;
				box-sizing: border-box;
				//padding: 0.1rem 0.3125rem;
				font-family: miui;
				z-index: 1;
			}
		}
	}
	
	.g-df-vc {
		-webkit-box-align: center;
		-webkit-align-items: center;
		-ms-flex-align: center;
		align-items: center;
		display: -webkit-box;
		display: -webkit-flex;
		display: -ms-flexbox;
		display: flex;
	}
	
	.coupon-box {
		color: #fe355d;
		font-weight: bold;
		font-size: 0.625rem;
	}
	
	.coupon {
		min-width: 80upx;
		width: 100upx;
		height: 40upx;
		background: linear-gradient(-90deg, #FE305C, #FF856F);
		border-radius: 10upx;
		font-weight: 400;
		padding: 0 8upx 0 2upx;
		box-sizing: border-box;
		display: inline-flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		-ms-flex-align: center;
		align-items: center;
		-webkit-box-pack: center;
		-webkit-justify-content: center;
		-ms-flex-pack: center;
		justify-content: center;
		color: #fff;
		margin-left: 0.375rem;
		font-size: 0.625rem;
	}
	
	.shop-img {
		width: 8rem;
		height: 8rem;
		-webkit-flex-shrink: 0;
		-ms-flex-negative: 0;
		flex-shrink: 0;
		border-radius: 0.3125rem;
		position: relative;
		margin: 0;
		padding: 0;
	
		.sdl-image {
			width: 95%;
			height: 95%;
			border-radius: 0.3125rem;
			border: none;
			vertical-align: middle;
		}
	}
	
	.btn-prev {
		margin-top: -0.25rem;
		color: #8d490b;
		-webkit-box-flex: 1;
		-webkit-flex: 1;
		-ms-flex: 1;
		flex: 1;
		text-align: center;
		font-size: 0.625rem;
	}
	
	.btn-next {
		margin-top: -0.25rem;
		color: #fff;
		width: 3.75rem;
		text-align: center;
		font-size: 0.75rem;
	}
	
	.g-df-c {
		-webkit-box-align: center;
		-webkit-align-items: center;
		-ms-flex-align: center;
		align-items: center;
		-webkit-box-pack: center;
		-webkit-justify-content: center;
		-ms-flex-pack: center;
		justify-content: center;
	}
	
	.top-sort {
		width: 2.125rem;
		height: 2.53125rem;
		background: #ccc;
		position: absolute;
		top: -0.3125rem;
		left: -0.375rem;
		//background: url(../../static/img/mainstay/top.png);
		background-size: contain;
		z-index: 2;
	}
	
	.sell {
		//font-size: 0.625rem;
		color: #000;
		margin-top: 5rpx;
		position: relative;
		display: flex;
		font-family: PingFang SC;
		font-size: 18rpx;
		font-weight: 400;
	}
	
	.oriprice {
		text-decoration: line-through;
		color: #999;
		margin-top: 0.1875rem;
		font-weight: 500;
		margin: 0.3125rem 0;
	}
	
	.g-text-ellipsis {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.shop-box {
		height: 240rpx;
		-webkit-box-flex: 1;
		-webkit-flex: 1;
		-ms-flex: 1;
		flex: 1;
		width: 50%;
		display: -webkit-box;
		display: -webkit-flex;
		display: -ms-flexbox;
		display: flex;
		-webkit-box-pack: justify;
		-webkit-justify-content: space-between;
		-ms-flex-pack: justify;
		justify-content: space-between;
		-webkit-box-orient: vertical;
		-webkit-box-direction: normal;
		-webkit-flex-direction: column;
		-ms-flex-direction: column;
		flex-direction: column;
		padding: 0.3125rem 0 0.1875rem;
		box-sizing: border-box;
		margin-left: 10rpx;
		border-bottom: 1px solid #e8e8e8;
		;
	
		.shop-padding {
			padding-left: 6upx;
			width: 100%;
			box-sizing: border-box;
	
			.sdl-text {
				font-family: PingFang SC;
				color: #333;
				font-size: 28rpx;
				font-weight: bold;
				width: 100%;
				box-sizing: border-box;
				padding-left: 0.1875rem;
				display: inline-block;
				line-height: 0.90625rem;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
	
	
	
	}
	
	.btn-gro {
		width: 12.5rem;
		height: 2.28125rem;
		border-radius: 0.3125rem;
		background-size: 100%;
		margin-left: -0.25rem;
		font-size: 0.625rem;
		font-weight: 500;
		padding: 0 0.3125rem;
		box-sizing: border-box;
	}
	
	.selectTop {
		text-align: center;
		color: #fff;
		border-bottom: 0.0625rem solid rgba(255, 255, 255, .3);
		padding-bottom: 0.625rem;
	}
	
	.tabs {
		border-bottom: 0.0625rem solid rgba(255, 255, 255, .3);
	}
	
	.header {
		//max-width: 23.4375rem;
		width: 100%;
		height: 5.9375rem;
		z-index: 100;
		position: relative;
		padding-top: 0.3125rem;
	
		.rank-head {
			width: 100%;
			background-repeat: repeat;
			background-size: 100% 100%;
	
		}
	}
	
	
	
	.stick-bg {
		background: linear-gradient(90deg, rgba(255, 154, 12, 0.9), rgba(253, 36, 39, 0.9));
		position: absolute;
		width: 100%;
		// max-width: 23.4375rem;
		height: 12.5rem;
		top: 0;
		-webkit-transition: background .3s;
		transition: background .3s;
	
	}
	
	
	.act-title {
		margin-top: 8rpx;
		margin-left: 12rpx;
		font-size: 26rpx;
		font-weight: bold;
		color: #767a82;
		font-family: PingFang SC;
	}
	
	.act-content {
		margin-left: 12rpx;
		font-size: 24rpx;
		color: #909399;
		font-family: PingFang SC;
	
		max-height: 40px;
		font-weight: bold;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}
	
</style>
